package cn.bs.busuo.hotel.service.impl;

import cn.bs.busuo.hotel.dao.persist.repository.HotelRepository;
import cn.bs.busuo.hotel.pojo.entity.*;
import cn.bs.busuo.hotel.pojo.param.*;
import cn.bs.busuo.hotel.pojo.vo.*;
import cn.bs.busuo.hotel.service.HotelService;
import cn.bs.busuo.common.enumerator.ServiceCode;
import cn.bs.busuo.common.ex.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class HotelServiceImpl implements HotelService {
    @Autowired
    private HotelRepository hotelRepository;
    @Override
    public void addNew(AddNewHotelParam addNewHotelParam) {
        //设置酒店实体类
        Hotel hotel = new Hotel();
        Byte a = 1;
        hotel.setEnable(a);
        BeanUtils.copyProperties(addNewHotelParam,hotel);
        //设置未设置城市的酒店实体类
        HotelsNotSetCity hotelsNotSetCity = new HotelsNotSetCity();
        hotelsNotSetCity.setHotelName(addNewHotelParam.getHotelName());
        hotelsNotSetCity.setHotelUrl(addNewHotelParam.getHotelUrl());
        hotelsNotSetCity.setHotelsId(hotel.getId());
        //设置未设置景点的酒店实体类
        HotelsNotSetSight hotelsNotSetSight = new HotelsNotSetSight();
        hotelsNotSetSight.setHotelName(addNewHotelParam.getHotelName());
        hotelsNotSetSight.setHotelUrl(addNewHotelParam.getHotelUrl());
        hotelsNotSetSight.setHotelsId(hotel.getId());
        int b = hotelRepository.selectHotelNameIfSimilar(hotel.getHotelName());
        if(b!=0){
            String msg = "酒店名已存在";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
        //添加酒店并添加未设置城市和景点的酒店
        addNew1(hotelsNotSetCity,hotel,hotelsNotSetSight);
    }

    @Override
    public void setPositionAboutSight(SetHotelPositionAboutSightParam setHotelPositionAboutSightParam) {
        if(hotelRepository.selectSightIfExist(setHotelPositionAboutSightParam.getSightsId())==0){
            String msg = "景点不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,msg);
        }
        SightsNearHotels sightsNearHotels = new SightsNearHotels();
        BeanUtils.copyProperties(setHotelPositionAboutSightParam,sightsNearHotels);
        setPositionAboutSight1(sightsNearHotels);
    }

    @Override
    public void setPositionAboutCity(SetHotelPositionAboutCityParam setHotelPositionAboutCityParam) {
        if(hotelRepository.selectCityIfExist(setHotelPositionAboutCityParam.getCityId())==0){
            String msg = "城市不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,msg);
        }
        CityHotels cityHotels = new CityHotels();
        BeanUtils.copyProperties(setHotelPositionAboutCityParam,cityHotels);
        setPositionAboutCity1(cityHotels);
    }

    @Override
    public void delete(Long hotelId) {
        int i = hotelRepository.deleteById(hotelId);
        if(i==0){
            String msg = "删除酒店失败,酒店不存在!!";
            throw new ServiceException(ServiceCode.ERROR_DELETE,msg);
        }
    }

    @Override
    public void update(UpdateHotelInformationParam updateHotelInformationParam) {
        if(hotelRepository.selectHotelIfEnable(updateHotelInformationParam.getId())==0){
            String msg = "酒店不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,msg);
        }
        if(hotelRepository.selectHotelNameIfSimilar(updateHotelInformationParam.getHotelName())==1){
            String msg = "酒店名称已被占用";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,msg);
        }
        Hotel hotel = new Hotel();
        BeanUtils.copyProperties(updateHotelInformationParam,hotel);
        int i = hotelRepository.updateById(hotel);
        if(i==0){
            String msg = "更新酒店信息失败";
            throw new ServiceException(ServiceCode.ERROR_UPDATE,msg);
        }
    }

    @Override
    public List<HotelVO> selectAll() {
        return hotelRepository.selectAll();
    }

    @Override
    public HotelVO selectById(Long hotelId) {
        return hotelRepository.selectById(hotelId);
    }

    @Override
    public List<HotelVO> selectHotelByCityId(Long cityId) {
        return hotelRepository.selectHotelByCityId(cityId);
    }

    @Override
    public List<HotelVO> selectHotelBySightId(Long sightId) {
        return hotelRepository.selectHotelBySightId(sightId);
    }

    @Override
    public List<HotelVO> selectHotelWhichNotSetCity() {
        return hotelRepository.selectHotelWhichNotSetCity();
    }

    @Override
    public List<HotelVO> selectHotelWhichNotSetSight() {
        return hotelRepository.selectHotelWhichNotSetSight();
    }

    @Override
    public void addHotelWorker(AddNewHotelWorkerParam addNewHotelWorkerParam) {
        if(hotelRepository.selectHotelWorkerIfExist(addNewHotelWorkerParam.getHotelId(),addNewHotelWorkerParam.getUserId())!=0){
            String msg = "酒店已存在该工作人员";
            throw new ServiceException(ServiceCode.ERROR_CONFLICT,msg);
        }
        UserRole userRole = new UserRole();
        userRole.setUserId(addNewHotelWorkerParam.getUserId());
        userRole.setRoleId(9L);
        HotelWorker hotelWorker = new HotelWorker();
        BeanUtils.copyProperties(addNewHotelWorkerParam,hotelWorker);
        addHotelWorker1( userRole,hotelWorker);
    }

    @Override
    public void deleteHotelWorker(DeleteHotelWorkerParam deleteHotelWorkerParam) {
        deleteHotelWorker1(deleteHotelWorkerParam);
    }

    @Override
    public List<HotelWorkerVO> selectHotelWorker(Long hotelId) {
        return hotelRepository.selectHotelWorker(hotelId);
    }

    @Override
    public void addRoomType(AddNewRoomTypeParam addNewRoomTypeParam) {
        HotelsRoomType hotelsRoomType = new HotelsRoomType();
        BeanUtils.copyProperties(addNewRoomTypeParam,hotelsRoomType);
        System.out.println(hotelsRoomType);
        int i = hotelRepository.addNewRoomType(hotelsRoomType);
        if(i==0){
            String msg = "添加房型失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
    }

    @Override
    public void deleteRoomType(DeleteHotelRoomTypeParam deleteHotelRoomTypeParam) {
        int i = hotelRepository.deleteRoomTypeById(deleteHotelRoomTypeParam);
        if(i==0){
            String msg = "删除房型失败";
            throw new ServiceException(ServiceCode.ERROR_DELETE,msg);
        }
    }

    @Override
    public List<HotelsRoomTypeCheckVO> selectRoomTypeWhichNotCheck() {
        return hotelRepository.selectRoomTypeWhichNotCheck();
    }

    @Override
    public List<HotelsRoomTypeCheckVO> selectRoomTypeWhichCheck() {
        return hotelRepository.selectRoomTypeWhichCheck();
    }

    @Override
    public void checkRoomTypeAccess(Long id) {
        int i = hotelRepository.checkRoomTypeAccess(id);
        if(i==0){
            String msg = "审核通过房型失败";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,msg);
        }
    }

    @Override
    public void checkRoomTypeRefused(Long id) {
        Byte a = 2;
        int i = hotelRepository.checkRoomTypeRefused(id, a);
        if(i==0){
            String msg = "审核拒绝房型失败";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,msg);
        }
    }

    @Override
    public List<HotelRoomTypeEnabledVO> selectHotelRoomType(Long hotelId) {
        int i = hotelRepository.selectHotelIfEnable(hotelId);
        if(i==0){
            String msg = "酒店不存在";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,msg);
        }
        return hotelRepository.selectHotelRoomType(hotelId);
    }

    @Override
    public void enableRoomType(Long id) {
        int i = hotelRepository.enableRoomType(id);
        if(i==0){
            String msg = "启用房型失败";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,msg);
        }
    }

    @Override
    public void disableRoomType(Long id) {
        int i = hotelRepository.disableRoomType(id);
        if(i==0){
            String msg = "禁用房型失败";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,msg);
        }
    }

    @Override
    public void addRoom(AddNewRoomParam addNewRoomParam) {
        int i = hotelRepository.addRoom(addNewRoomParam);
        if(i!=1){
            String msg = "添加房间失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
    }

    @Override
    public void deleteRoom(DeleteRoomParam deleteRoomParam) {
        int i = hotelRepository.deleteRoom(deleteRoomParam);
        if(i!=1){
            String msg = "减少房间失败";
            throw new ServiceException(ServiceCode.ERROR_DELETE,msg);
        }
    }

    @Transactional
    public void addNew1(HotelsNotSetCity hotelsNotSetCity,Hotel hotel,HotelsNotSetSight hotelsNotSetSight) {
        int i = hotelRepository.addNew(hotel);
        if(i==0){
            String msg = "添加酒店失败,添加酒店表失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
        Long id = hotelRepository.selectIdByHotelName(hotel.getHotelName());
        hotelsNotSetCity.setHotelsId(id);
        hotelsNotSetSight.setHotelsId(id);
        i = hotelRepository.addNewHotelToNotSetCity(hotelsNotSetCity);
        if(i==0){
            String msg = "添加酒店失败,添加未设置城市酒店表失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
        i = hotelRepository.addNewHotelToNotSetSight(hotelsNotSetSight);
        if(i==0){
            String msg = "添加酒店失败,添加未设置景点酒店表失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
    }
    @Transactional
    public void setPositionAboutSight1(SightsNearHotels sightsNearHotels) {
        int i = hotelRepository.deleteHotelsNotSetSight(sightsNearHotels.getHotelsId());
        if(i==0){
            String msg = "删除未设置景点酒店表失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
        i = hotelRepository.setPositionAboutSight(sightsNearHotels);
        if(i==0){
            String msg = "设置景点附近的酒店失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
    }
    @Transactional
    public void setPositionAboutCity1(CityHotels cityHotels) {
        int i = hotelRepository.deleteHotelsNotSetCity(cityHotels.getHotelId());
        if(i==0){
            String msg = "删除未设置城市酒店表失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
        i = hotelRepository.setPositionAboutCity(cityHotels);
        if(i==0){
            String msg = "设置酒店城市失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
    }
    public void addHotelWorker1(UserRole userRole,HotelWorker hotelWorker) {
        int i = hotelRepository.addHotelWorker(hotelWorker);
        if(i==0){
            String msg = "添加酒店工作人员失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
        i = hotelRepository.addHotelWorkerRole(userRole);
        if(i==0){
            String msg = "添加酒店工作人员角色失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
    }
    @Transactional
    public void deleteHotelWorker1(DeleteHotelWorkerParam deleteHotelWorkerParam) {
        int i = hotelRepository.deleteHotelWorker(deleteHotelWorkerParam);
        if(i==0){
            String msg = "删除酒店工作人员失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
        i = hotelRepository.deleteHotelWorkerRole(deleteHotelWorkerParam.getUserId());
        if(i==0){
            String msg = "删除酒店工作人员角色失败";
            throw new ServiceException(ServiceCode.ERROR_INSERT,msg);
        }
    }
}
